package DAO;

import Exceptions.DaoException;
import Interfaces.ILogin_Dao;
import Models.Login;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Login_Dao implements ILogin_Dao {

    @Override
    public Login ValidateLogin(String Name, String Pass) throws DaoException {
        Login r = new Login();
        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement pst = null;
        try {
            conn = DAO.javaconnect.ConnecrDb("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/nlr");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Login_Dao.class.getName()).log(Level.SEVERE, null, ex);
        }

        String sql = "select * from  user where username=? and pass=?"; // Το table , username, password ειναι ονομασιες που εδωσα στη βαση μου
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, Name);
            pst.setString(2, Pass);

            rs = pst.executeQuery();
            if (rs.next()) {

                r.setName(rs.getString("username"));
                r.setPass(rs.getString("pass"));

            }

        } catch (SQLException e) {
            return null;
        }

        return r;
    }
}
